﻿<%@ Page Title="Company" Language="VB" MasterPageFile="~/MasterPage.master" Theme="MSN_Morning" %>

<script runat="server">    
    Dim company1 As Inventory.companys = New Inventory.companys()
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    End Sub
    
    Public Sub cleanfields()        
        txt_id_company.Text = "0"
        txt_name.Text = ""
        txt_website.Text = ""
        txt_active.Text = "True"
    End Sub   
    Protected Sub btn_clear_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        cleanfields()
    End Sub    
    Protected Sub btn_insert_Click(ByVal sender As Object, ByVal e As System.EventArgs)        
        formfieldtovariable()     
        lbl_info.Text = "Success." + company1.insert_company()
        lbl_info.ForeColor = Drawing.Color.Green        
    End Sub
    Protected Sub btn_update_Click(ByVal sender As Object, ByVal e As System.EventArgs)        
        formfieldtovariable()
        lbl_info.Text = "Success." + company1.update_company()
        lbl_info.ForeColor = Drawing.Color.Green        
    End Sub
    Public Sub variabletoformfield() ' coloca os campos da variavel para o form
        txt_id_company.Text = company1.id_company.ToString()
        txt_name.Text = company1.name
        txt_website.Text = company1.website
        txt_active.Text = company1.active.ToString()
    End Sub    
    Public Sub formfieldtovariable() ' coloca os campos do formulario na variável
        company1.id_company = Integer.Parse(txt_id_company.Text)
        company1.name = txt_name.Text
        company1.website = txt_website.Text
        If txt_active.Text = "True" Then
            company1.active = True
        Else
            company1.active = False
        End If
    End Sub    
    Protected Sub btn_last_Click(ByVal sender As Object, ByVal e As System.EventArgs)        
        company1.last_company()
        variabletoformfield()        
    End Sub    
    Protected Sub btn_first_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        company1.first_company()
        variabletoformfield()
    End Sub
    Protected Sub btn_next_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        formfieldtovariable()
        company1.next_company()
        variabletoformfield()        
    End Sub
    Protected Sub btn_previous_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        formfieldtovariable()
        company1.previous_company()
        variabletoformfield()
        cleanfields()
    End Sub
    Protected Sub btn_delete_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        formfieldtovariable()
        company1.delete_company()
        variabletoformfield()
    End Sub
    
    Protected Sub Page_LoadComplete(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim connection_string As String = ConfigurationManager.ConnectionStrings("conn").ConnectionString
        Dim SqlConnection1 As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection()
        SqlConnection1.ConnectionString = connection_string
        SqlConnection1.Open()
        Dim SqlCommand1 As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand()
        Dim string_sql As String = "SELECT * FROM companys"
        SqlCommand1.Connection = SqlConnection1
        SqlCommand1.CommandText = string_sql
        GridView1.DataKeyNames = New String() {"id_company"}
        GridView1.DataSource = SqlCommand1.ExecuteReader()
        GridView1.DataBind()
        SqlConnection1.Close()
    End Sub

    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)        
        
        'Page.ClientScript.RegisterStartupScript("".GetType(), "ClientScript", "alert('" + GridView1.SelectedValue.ToString() + "');", True)

        'txt_id_company.Text = GridView1.SelectedValue.ToString()
        'txt_name.Text = GridView1.SelectedRow.Cells(2).Text
        'txt_website.Text = GridView1.SelectedRow.Cells(3).Text
        'txt_active.Text = GridView1.SelectedRow.Cells(4).Text        
        
        company1.id_company = GridView1.SelectedValue.ToString()
        company1.select_company_by_id() 'vai aceder à base de dados
        variabletoformfield()
        
        'Avaliação
        'insert - 10%
        'update - 10%
        'delete - 10%
        'botões de navegação - 10%
        'gridview - 20%
        'classe - 20%
        'aspecto visual - 10%
        'utilização de um dll - 10%
        
    End Sub



    Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs)
        'numero da linha
        Page.ClientScript.RegisterStartupScript("".GetType(), "ClientScript", "alert('id:" + GridView1.Rows(e.RowIndex).Cells(2).Text + "');", True)
        'proximo passo: id????????????????????????????????
        
        
    End Sub
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">

</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <h1>Companys</h1>
    <table width="100%">
        <tr>
            <td>
                <asp:Label ID="lbl_id_company" runat="server" Text="id_company"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txt_id_company" runat="server" Text="0" ReadOnly="true"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="lbl_name" runat="server" Text="name"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txt_name" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="lbl_website" runat="server" Text="website"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txt_website" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                <asp:Label ID="lbl_active" runat="server" Text="active"></asp:Label>
            </td>
            <td>
                <asp:TextBox ID="txt_active" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td></td>
            <td>
                <asp:Button ID="btn_clear" runat="server" Text="Clear" onclick="btn_clear_Click" />
                <asp:Button ID="btn_insert" runat="server" onclick="btn_insert_Click" Text="Insert" />                
                <asp:Button ID="btn_update" runat="server" Text="Update" onclick="btn_update_Click" />
                
                <asp:Button ID="btn_first" runat="server" Text="<<" onclick="btn_first_Click" />
                <asp:Button ID="btn_previous" runat="server" Text="<" onclick="btn_previous_Click" />
                <asp:Button ID="btn_next" runat="server" Text=">" onclick="btn_next_Click" />
                <asp:Button ID="btn_last" runat="server" Text=">>" onclick="btn_last_Click" />
                
                <asp:Button ID="btn_delete" runat="server" Text="Delete" onclick="btn_delete_Click" />

                <asp:Label ID="lbl_info" runat="server"></asp:Label>
                
            </td>
        </tr>
        <tr>
            <td colspan="2">            
                <asp:GridView ID="GridView1" runat="server" 
                    onselectedindexchanged="GridView1_SelectedIndexChanged" 
                    onrowdeleting="GridView1_RowDeleting">
                    <Columns>
                        <asp:CommandField HeaderText="Select" ShowHeader="True" ShowSelectButton="True" />
                        <asp:CommandField ButtonType="Button" HeaderText="Delete" ShowDeleteButton="True" ShowHeader="True" />
                    </Columns>
                </asp:GridView>            
            </td>
        </tr>                
    </table>
</asp:Content>

